import random

# 定义卡牌类型
card_types = ["羊", "草", "狼"]

# 初始化游戏板
def init_board():
    board = []
    for _ in range(9):
        card = random.choice(card_types)
        board.append(card)
    return board

# 显示游戏板
def display_board(board):
    print("当前游戏板：")
    for i in range(0, len(board), 3):
        print(" ".join(board[i:i + 3]))

# 检查是否有三张相同卡牌可消除
def can_remove(board, index):
    card = board[index]
    count = board.count(card)
    return count >= 3

# 消除三张相同卡牌
def remove_cards(board, card):
    for _ in range(3):
        board.remove(card)
    return board

# 检查游戏是否胜利
def is_win(board):
    return len(board) == 0

# 主游戏循环
def main():
    board = init_board()
    while True:
        display_board(board)
        try:
            index = int(input("请输入要选择的卡牌索引（0 - {}），输入 -1 退出游戏：".format(len(board) - 1)))
            if index == -1:
                print("游戏退出。")
                break
            if 0 <= index < len(board):
                if can_remove(board, index):
                    card = board[index]
                    board = remove_cards(board, card)
                    if is_win(board):
                        print("恭喜你，游戏胜利！")
                        break
                else:
                    print("没有三张相同的卡牌，无法消除。")
            else:
                print("输入的索引无效，请重新输入。")
        except ValueError:
            print("输入无效，请输入一个整数。")

if __name__ == "__main__":
    main()